package mundo.java.client.view;

import mundo.java.client.Artigo.Messages;
import mundo.java.client.events.GreetingEvent;
import mundo.java.client.events.MessageValidatedEvent;
import mundo.java.client.events.Event.Handler;
import mundo.java.client.images.Banners;
import mundo.java.client.images.Icons;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.http.client.UrlBuilder;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ToggleButton;

public abstract class ArtigoAbstractView {
	TextBox nameField = new TextBox();
	Button sendButton;
	Label errorLabel = new Label();

	final HandlerManager eventBus;
	final Messages messages;

	public abstract String greet(String text, String serverInfo, String userAgent);

	public ArtigoAbstractView(HandlerManager eventBus, Messages messages) {
		this.eventBus = eventBus;
		this.messages = messages;
		
		sendButton = new Button(messages.sendButtonLabel());
		
		eventBus.addHandler(MessageValidatedEvent.TYPE, new Handler<MessageValidatedEvent>() {
			@Override
			public void onEvent(MessageValidatedEvent event) {
				if (event.messageValidated)
					sendButton.setEnabled(false);
				errorLabel.setVisible(!event.messageValidated);
			}
		});
	}
	
	public void render() {
		Window.setTitle(messages.heading());
		RootPanel.get("header").getElement().setInnerText(messages.heading());
		RootPanel.get("textboxLabel").getElement().setInnerText(messages.textboxLabel());

		// Add the nameField, sendButton and errorLabel to the RootPanel
		RootPanel.get("nameFieldContainer").add(nameField);
		RootPanel.get("sendButtonContainer").add(sendButton);
		RootPanel.get("errorLabelContainer").add(errorLabel);
		errorLabel.setText(messages.validationMessage());
		errorLabel.setVisible(false);
		
		nameField.setText(messages.defaultTextValue());

		// Focus the cursor on the name field when the app loads
		nameField.setFocus(true);
		nameField.selectAll();

		sendButton.addStyleName("sendButton");
		
		Interactor interactor = new Interactor();
		nameField.addKeyUpHandler(interactor);
		sendButton.addClickHandler(interactor);
		
		insertImages();
	}
	
	public static interface Images extends Banners, Icons { }
	private final Images images = GWT.create(Images.class);

	private void insertImages() {
		RootPanel.get("logo").add(new Image(images.logo()));
		RootPanel.get("guj").add(new Image(images.guj()));
		
		HorizontalPanel localePanel = new HorizontalPanel();
		final ToggleButton us = new ToggleButton(new Image(images.us()));
		final ToggleButton br = new ToggleButton(new Image(images.br()));

		String locale = Window.Location.getParameter("locale");
		final boolean ptBr = locale!=null && locale.equals("pt_BR");
		us.setDown(!ptBr);
		br.setDown(ptBr);
		
		class LocaleHandler implements ClickHandler {
			private boolean localeChanged;

			@Override
			public void onClick(ClickEvent event) {
				UrlBuilder urlBuilder = Window.Location.createUrlBuilder();
				ToggleButton source = (ToggleButton) event.getSource();
				if (source.equals(br)) {
					us.setDown(false);
					localeChanged = !ptBr;
					urlBuilder.setParameter("locale", "pt_BR");
				}
				else {
					br.setDown(false);
					localeChanged = ptBr;
					urlBuilder.removeParameter("locale");
				}
				if (localeChanged)
					Window.Location.replace(urlBuilder.buildString());
				else source.setDown(true);
			}
		}
		LocaleHandler handler = new LocaleHandler();
		us.addClickHandler(handler);
		br.addClickHandler(handler);
		
		localePanel.add(us);
		localePanel.add(br);
		RootPanel.get("locale").add(localePanel);
	}

	// Create a handler for the sendButton and nameField
	class Interactor implements ClickHandler, KeyUpHandler {
		public void onClick(ClickEvent event) {
			sendNameToServer();
		}

		public void onKeyUp(KeyUpEvent event) {
			if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER)
				sendNameToServer();
		}

		private void sendNameToServer() {
			eventBus.fireEvent(new GreetingEvent(nameField.getValue()));
		}
	}
}